package com.example.es.util;

import com.example.es.pojo.Content;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;

public class HtmlParseUtil {


	public static ArrayList<Content> paresJD(String keyword) throws IOException {
		String url="https://search.jd.com/Search?keyword="+keyword;
		Document document=Jsoup.parse(new URL(url),30000);
		Element element=document.getElementById("J_goodsList");

		Elements elements=element.getElementsByTag("li");

		ArrayList<Content> result=new ArrayList<>();
		for (Element el : elements) {
			String img=el.getElementsByTag("img").eq(0).attr("data-lazy-img");
			String price=el.getElementsByClass("p-price").eq(0).text();
			String title=el.getElementsByClass("p-name").eq(0).text();

			result.add(new Content(img,title,price));
		}
		return  result;
	}

}
